package com.fly.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fly.demo.entity.User;
import com.fly.demo.service.UserService;
import com.ketayao.ketacustom.util.dwz.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * UserController
 * 
 * @author 00fly
 * @version [版本号, 2018-09-11]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@RestController
@Api(tags = "用户操作接口")
@RequestMapping("/rest/user")
public class UserRestController
{
    @Autowired
    UserService userService;
    
    /**
     * 新增/更新数据
     * 
     * @param user
     * @return
     * @see [类、类#方法、类#成员]
     */
    @ApiOperation("新增用户")
    @PostMapping("/add")
    public String add(@RequestBody User user)
    {
        userService.saveOrUpdate(user);
        return "success";
    }
    
    /**
     * 列表展示
     * 
     * @param model
     * @return
     * @see [类、类#方法、类#成员]
     */
    @ApiOperation("分页查询用户")
    @GetMapping({"/", "/list"})
    public org.springframework.data.domain.Page<User> list(@RequestParam(defaultValue = "1") int pageNo)
    {
        pageNo = Math.max(pageNo, 1);
        Page page = new Page();
        page.setPageNum(pageNo);
        return userService.findAll(page);
    }
    
}
